package com.sdry.web.controller.workplace;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.hpf.Product;
import com.sdry.model.lz.DeviceModel;
import com.sdry.model.workplace.Assemble;
import com.sdry.model.workplace.Centrifugal;
import com.sdry.model.workplace.CentrifugalExample;
import com.sdry.model.workplace.CentrifugalExample.Criteria;
import com.sdry.service.workplace.CentrifugalServise;
import com.sdry.service.workplace.OthersService;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName: OthersController
 * @Description: 生产工位相关
 * @author tdd
 * @date 2018年8月29日
 * @version V1.0
 */
@Controller
@RequestMapping("/others")
public class OthersController {
	 @Resource
	 private OthersService othersService;

	 /**
     * 条件查询设备
     * @param response 响应
     * @param deviceModel 设备信息
     * @throws Exception 异常
     */
    @RequestMapping("/deviceByMution")
    public void selectmousseSampleById(HttpServletResponse response, DeviceModel deviceModel) throws Exception {
		List<DeviceModel> devices = othersService.deviceByMution(deviceModel);
		//将对象转化为json数组
        JSONArray json = JSONArray.fromObject(devices);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改设备信息
     * @param response 响应
     * @param deviceModel 设备信息
     * @throws Exception 异常
     */
    @RequestMapping("/deviceUpdate")
    public void editmousseSample( HttpServletResponse response,
    							DeviceModel deviceModel) throws Exception {
		int affct = 0;
    	List<DeviceModel> devices = othersService.deviceByMution(deviceModel);
        if(devices.size() == 1){
        	deviceModel.setId(devices.get(0).getId());
        	deviceModel.setRemark05("绑定");
            //修改
            affct = othersService.deviceUpdate(deviceModel);
        }
        ResponseUtil.write(response, affct);
    }
    /**
     * 根据时间/产品/膜管编号查组装膜管（分页）
     * @param response 响应
     * @param deviceModel 设备信息
     * @throws Exception 异常
     */
    @RequestMapping("/pageAssembleAtubeList")
    public void pageAssembleAtubeList( HttpServletResponse response,
    		 @RequestParam(value = "page",required = false)int page,
             @RequestParam(value = "rows",required = false)int rows,
             Assemble assemble,String product) throws Exception {
    	/*创建分页工具类*/
    	PageHelper.startPage(page, rows);

		Map<String, Object> map = new HashMap<>();
		if (assemble.getAtube() != null) {
			map.put("atube", assemble.getAtube());
		} else {
			map.put("atube", "");
		}
		if (assemble.getAtime() != null) {
			map.put("atime", assemble.getAtime());
		} else {
			map.put("atime", "");
		}
		if (product != null) {
			map.put("product", product);
		} else {
			map.put("product", "");
		}
		//是否删除
		map.put("aremark", "0");
		
		List<Assemble> assembles = othersService.selectAtubeByDateAndProduct(map);
		
		PageInfo<Assemble> info = new PageInfo<Assemble>(assembles);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }

    @RequestMapping("/findProductByPnum")
    public void findProductByPnum( HttpServletResponse response,String product) throws Exception {
    	Product products = othersService.findProductByPnum(product);
    	int num  = Integer.valueOf(products.getClassify());
    	ResponseUtil.write(response,num);
    }
}
